<?php 
/*
 * 列表页
* @arrange Sameal
* @package ixdcw
* @file    list.inc.php
* @time    13-3-14 下午1:15
*/
defined('IN_IXDCW') or exit('Access Denied');

require DT_CORE.'/'.MODULE_NAME.'/common.inc.php';

ViewCheck::catExists() or Prompt::listNotFound();
ViewCheck::groupListPermission() or Prompt::noPermission();

// normal
$filter['catid']  = getgpc('catid');
$filter['areaid'] = getgpc('areaid');
$filter['typeid'] = getgpc('typeid');

// auto
$filter['ctp'] = getgpc('ctp');
$filter['bid'] = getgpc('bid');
$filter['cid'] = getgpc('cid');
$filter['mid'] = getgpc('mid');
$filter['cen'] = getgpc('cen');

$order  = isset($order) ? intval($order) : 0;
$TYPES = explode('|', $MOD['type']);

require_once DT_INC . '/search/listAuto.inc.php';

$maincat = Search::mainCat(MODULE_NAME, $catid);
if (!empty($ARE)) {

    $mainarea=Xd_get_arealists($ARE);

} else {
    $mainarea[0] = get_mainarea(0);
}


$areaParentid = $ARE['parentid'];
if ($kw) {
    SearchCheck::length($kw);
    SearchCheck::timeLimit();
}

// condition
$condition = "(status=3 or status=2)";

if ($areaid) {
    $condition .=  $ARE['child'] ? " AND areaid IN (".$ARE['arrchildid'].") " : " AND areaid=$areaid ";
}
if ($kw) {
    $condition .= " AND title LIKE '%$kw%'";
}
if (!empty($CAT)) {
    $condition .= ($CAT['child']) ? " AND catid IN (" . $CAT['arrchildid'] . ")" : " AND catid=$catid ";
}
if ($filter['typeid'] && array_key_exists($filter['typeid'], $TYPES)) {
	$condition .= " AND typeid={$filter['typeid']} ";
}

if($cityid&&$cityid!=0){
	$city_order='(case when areaid IN ('.$AREA[$cityid]["arrchildid"].') then 1 else 2 end ),';
}else{
	$city_order='';
}

if ($autoFilter) {

	$carids   = array_unique((array) __($MODELS)->pluck('arcid'));
	$brandids = array_unique((array) __($MODELS)->pluck('arbid'));
	$modelids = array_unique((array) __($MODELS)->pluck('amid'));

	$carids   = $carids ? $carids : array('-');
	$brandids = $brandids ? $brandids : array('-');
	$modelids = $modelids ? $modelids : array('-');

	$condition .= " and amid in (" . Sql::in($modelids) . ")";
	$condition .= " and arcid in (" . Sql::in($carids) . ")";
	$condition .= " and arbid in (" . Sql::in($brandids) . ")";

}
// order

$ORDER = array($MOD['order'], /*'groupid desc', 'groupid asc', */'addtime DESC'/*, 'addtime ASC'*/);
isset($ORDER[$order]) or $order = 0;

// page
$total    = $db->count($table, $condition, $DT['cache_search']);

$do = new BuyModule($moduleid);

if ($total) {
    $tags=$do->getList($condition,$city_order.$ORDER[$order],($DT['cache_search'] && $page == 1) ? 'CACHE' : '', $DT['cache_search']);
    if ($page == 1 && $kw) keyword($kw, $items, $moduleid);
}

foreach($tags as $v=>$k ){
    $username=$k;
    $user = userinfo($username['username']);
    $userid=$user['userid'];
    $r = $db->getOne("SELECT *FROM {$db->getPre()}member WHERE userid=$userid");
    $tags[$v]['company']=$r['company'];
    $online = online($userid);
    $tags[$v]['online']=$online;
}
$showpage = 1;
$datetype = 5;

$seo_file = 'list';
include DT_INC.'/seo.inc.php';

$template = $CAT['template'] ? $CAT['template'] : 'list';
include template($template, $module);